<?php
/**
 *异动日志
 */
apf_require_class("Bll_BLL");
apf_require_class("Bll_Hr_User");
apf_require_class("Bll_Hr_Job");
apf_require_class("Bll_Hr_Department");

class Bll_Hr_UnusualLog extends BLL
{

    /**
     * 异动日志按类型获取用户最后信息
     *
     * @param array $uids
     * @param int $type
     */
    public function get_log_by_uid ($uids, $type)
    {
        $data = array();
        if (empty($uids) || empty($type)) {
            return $data;
        }
        $where = array(
                "user_id IN(" . implode(',', $uids) . ") AND 1=?" => 1
        );
        $where["reason_type"] = $type;
        $data = $this->get_log_dao()->find($where, '', 0);
        return $data;
    }

    /**
     * 写入转岗 离职日志
     * TODO晋升
     *
     * @param int $uid
     * @param int $job_id
     *            转岗为新职位id,离职为原职位id
     * @param int $parent_uid
     * @param int $parent_job_id
     * @param int $type
     *            1转岗 2晋升 3离职
     */
    public function save_unusual_log ($uid, $job_id, $parent_id, $parent_job_id, $type)
    {
        $status = false;
        $user_data = $this->get_user_bll()->get_user_by_id($uid);
        if (empty($user_data)) {
            return $status;
        }
        $job_data = $this->get_job_bll()->get_job_dep_info($job_id);
        $in = array(
                'user_id' => $uid,
                'department_id' => $job_data['department_id'],
                'department_name' => $job_data['department_name'],
                'job_id' => $job_data['job_id'],
                'reason_type' => $type,
                'parent_id' => $parent_id,
                'parent_job_id' => $parent_job_id,
                'p_level' => $user_data['p_level'],
                'm_level' => $user_data['m_level'],
                'created' => time()
        );
        return $this->get_log_dao()->insert($in);
    }
    // 部门bll
    private $_dep_bll = null;
    // Job bll
    private $_job_bll = null;
    // user bll
    private $_user_bll = null;

    private function get_dep_bll ()
    {
        if (! isset($this->dep_bll)) {
            $this->_dep_bll = new Bll_Hr_Department();
        }
        return $this->_dep_bll;
    }

    private function get_job_bll ()
    {
        if (! isset($this->_job_bll)) {
            $this->_job_bll = new Bll_Hr_Job();
        }
        return $this->_job_bll;
    }

    private function get_user_bll ()
    {
        if (! isset($this->_user_bll)) {
            $this->_user_bll = new Bll_Hr_User();
        }
        return $this->_user_bll;
    }

    private function get_log_dao ()
    {
        $dao = $this->get_dao('Dao_Hr_UnusualLog');
        return $dao;
    }
}